<?php
class watermarkclass {

	function watermarkclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function image_add($inf, $name, $link, $parent, $w, $h, $usr) {
		$dat = date("YmdHis");
		$info['name'] = $link;
		$info['type'] = $inf['type'];
		$info['size'] = $inf['size'];
		$info['height'] = $h;
		$info['width'] = $w;
		$info = serialize($info);
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."files` (`id`,`parent`,`name`,`info`,`save_date`,`owner`, `types`)	VALUES ('', '$parent', '$name', '$info', '$dat', '$usr', 'imagew')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		$i = mysql_insert_id();
		return $i;
	}
	
	function image_del($parent, $name="", $id="") {
		global $setupdata, $treeclass;
		if ($name == ""){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."data` WHERE `parent` = '".$parent."' AND `value`='".$id."' AND `type`='imagew'", __LINE__, __FILE__);
			$num = mysql_num_rows($sql);
			if ($num == 1){
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$treeclass->mod_item_data($parent, $row['name'], "");
			}
		} else $treeclass->mod_item_data($parent, $name, "");
		if ($id > 0) {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE `id` = ".$id, __LINE__, __FILE__);
			$row = mysql_num_rows($sql);
			if ($row != 0) {
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$parent = $row['parent'];
				$row = unserialize($row['info']);
				$files = $setupdata['binary_path']."/".$row['name'];
				if (!(file_exists($files))) {
					$setupdata['binary_path'] = $setupdata['binary_path']."/".$parent;
					$files = $setupdata['binary_path']."/".$row['name'];
				}
				if ((file_exists($files)) and (strlen($row['name']) > 0)){
					list($par, $d, $s) = explode("-", $row['name']);
					$pattern = $par."-".$d;
					if (is_dir($setupdata['binary_path'])){
						$d = opendir($setupdata['binary_path']);
						while ($file = readdir($d)){
							if (is_file($setupdata['binary_path']."/".$file)) {
								if (strpos($file, $pattern) === FALSE){
								} else{
									unlink($setupdata['binary_path']."/".$file);
								}
							}
						}
						closedir($d);
					}
				}
				if (file_exists($files) === FALSE ){
					$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."files` WHERE `id` = ".$id, __LINE__, __FILE__);
				}
			}
		}
	}

	function watermark_add($inf, $name, $link, $w, $h, $usr) {
		$dat = date("YmdHis");
		$info['name'] = $link;
		$info['type'] = $inf['type'];
		$info['size'] = $inf['size'];
		$info['height'] = $h;
		$info['width'] = $w;
		$info = serialize($info);
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."watermark_files` (`id`,`name`,`info`,`save_date`,`owner`, `x`, `y`)	VALUES ('', '".$name."', '".$info."', '".$dat."', '".$usr."', 0, 0)";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
	}
	
	function get_all_watermarks(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."watermark_files` ORDER BY `save_date` DESC", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$row['info'] = unserialize($row['info']);
			$result[$row['id']] = $row;
		}
		return $result;
	}
	
	function get_watermark_data($id){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.* FROM `".$this->setupdata['table_prefix']."watermark_files` `t1` WHERE ".$id."=`t1`.`id`", __LINE__, __FILE__);			
		if (mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$row['name'] = stripslashes($row['name']);
			$row['info'] = unserialize($row['info']);
			$result = $row;
		}
		return $result;
	}
	
	function rename_watermark($parent, $name){
		if (isset($parent) && (strlen($parent) > 0)){
			if (isset($name) && (strlen($name) > 0)){
				$name = trim($name);
				$name = addslashes($name);
				$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."watermark_files` SET `name`='".$name."' WHERE `id` = ".$parent, __LINE__, __FILE__);
			}
		}
	}
	
	function mod_watermark($parent, $x, $y){
		$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."watermark_files` SET `x`='".floatval($x)."', `y`='".floatval($y)."' WHERE `id` = ".$parent, __LINE__, __FILE__);
	}
	
	function get_watermark_data_by_page_id($id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."data` WHERE ".$id."=`parent` AND `type`='watermark' AND `value`!=''", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."watermark_files` WHERE ".$row['value']."=`id`", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$row['info'] = unserialize($row['info']);
				return $row;
			}
		}
		return NULL;
	}
	
	function watermark_del($arr) {
		foreach($arr as $id){
			if (isset($id) and ($id > 0)){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."watermark_files` WHERE `id` = ".$id, __LINE__, __FILE__);
				if (mysql_num_rows($sql) != 0) {
					$row = mysql_fetch_array($sql, MYSQL_ASSOC);
					$row = unserialize($row['info']);
					$file = $this->setupdata['binary_path']."/watermark/".$row['name'];
					if (file_exists($file)) unlink($file);
					if (file_exists($file) === FALSE){
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."watermark_files` WHERE `id` = ".$id, __LINE__, __FILE__);
					}
				}
			}
		}
	}
}
?>
